;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2020 Maxime Devos <maxime.devos@student.kuleuven.be>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

;; TODO integrate in Guix upstream when in reasonable shape
(use-modules (gnu packages gnunet)
             (gnu packages guile)
             (gnu packages guile-xyz)
             (gnu packages autotools)
             (gnu packages pkg-config)
             (gnu packages gnupg)
             (gnu packages compression)
             (gnu packages texinfo)
             (gnu packages)
             (guix build-system gnu)
             (guix git-download)
             (guix packages)
             (guix gexp)
             ((guix licenses) #:prefix license:))

;; some bugs to squash:
;;  guile-gnunet installs .go in $GUILE_LOAD_PATH

(define debug? #t)
;; guix commit: 0ed6c7444e9a5509df11d4802bb257f3bc883733
;; TODO: guile bindings

(define gnunet/debug
  (package
    (inherit gnunet)
    (outputs '("out"))
    (arguments `(#:tests? #f
                 #:strip-binaries? #f
                 #:configure-flags
                 `("--without-x" "--without-libpulse"
                   "--without-libogg" "--without-libopus"
                   "--disable-documentation")
                 #:make-flags
                 `("CFLAGS=-Og -g3")))))

;; cross-compilation is broken,
;; but ordinary compilation isn't
(define guile2.2-bytestructures/broken
  (package
    (inherit guile2.2-bytestructures)
    (version "1.0.7")
    (name (package-name guile2.2-bytestructures))
    (home-page (package-home-page
                guile2.2-bytestructures))
    (source
     (origin
       (method git-fetch)
       (uri (git-reference
             (url home-page)
             (commit (string-append "v" version))))
       (file-name (git-file-name name version))
       (sha256
        (base32
         "0q0habjiy3h9cigb7q1br9kz6z212dn2ab31f6dgd3rrmsfn5rvb"))))))

(define guile2.2-webutils
  (package
    (inherit guile-webutils)
    (name "guile2.2-webutils")
    (inputs
     `(("guile" ,guile-2.2)))
    (propagated-inputs
     `(("guile-irregex" ,guile2.2-irregex)
       ("guile-gcrypt" ,guile2.2-gcrypt)))))

(define guile-gnunet/fixed
  (let ((commit "dbc873b4a1041a51e0e342cae178cb2fda014ab6")
        (revision "1"))
    (package
      (inherit guile-gnunet)
      (name (package-name guile-gnunet))
      (version (git-version "0.0" revision commit))
      (source (origin
                (method git-fetch)
                (uri (git-reference
                      (url "https://git.gnunet.org/gnunet-guile2.git")
                      (commit commit)))
                (file-name (git-file-name name version))
                (sha256
                 (base32
                  "0azsdi8sa5kvv7qwc0b096w4a5ggfzcmi4kb06fb4sc59f36fzmq"))))
      (native-inputs `(("pkg-config" ,pkg-config)
                       ("autoconf" ,autoconf-wrapper)
                       ("texinfo" ,texinfo)
                       ("automake" ,automake)))
      (inputs `(("guile" ,guile-2.2)
                ("gnunet" ,gnunet/debug)))
      (propagated-inputs
       `(("guile-bytestructures" ,guile2.2-bytestructures/broken)))
      (home-page "https://gnu.org/software/gnunet"))))

(define rehash
  (package
    (name "rehash")
    (version "0")
    (source #f)
    (build-system gnu-build-system)
    (inputs `(("gnunet" ,(if debug? gnunet/debug gnunet))
              ("guile" ,guile-3.0) ; guile-gnunet uses guile-2.0!
              ,@(if debug?
                    `(("guile:debug" ,guile-2.2 "debug"))
                    '())
              ("guile-fibers" ,guile-fibers)
              ;; GNUnet headers refer to extractor.h
              ("libextractor" ,libextractor)
              ;; likewise for gcrypt
              ("libgcrypt" ,libgcrypt)
              ;; perhaps a bug in GNUnet's pkg-config files
              ("zlib" ,zlib)
              ;; for web demo
              ;; TODO guile2.0-gcrypt FTB
              ("guile-webutils" ,guile-webutils)))
    (native-inputs
     `(("autoconf" ,autoconf)
       ("automake" ,automake)
       ("libtool"  ,libtool)
       ("guile" ,guile-3.0)
       ("pkg-config" ,pkg-config)
       ,@(if debug?
             `(("gdb" ,(specification->package "gdb")))
             '())))
    (synopsis "Map hashes to other hashes with the GNUnet DHT")
    (license license:gpl3+)
    (home-page "https://notabug.org/mdevos/guix-gnunet")
    ;; TODO: somewhat malicious decentralised hash->hash mapping
    ;; operating on GNUnet
    (description "")))

rehash
